package com.taskcollection.control;

import com.alibaba.fastjson.JSONObject;
import com.taskcollection.bean.ProjectBean;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.bean.TeacherBean;
import com.taskcollection.service.ProjectService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by haocongshun on 2016/8/18.
 */
public class ProjectControl {
    private ProjectService prs;

    public ProjectService getPrs() {
        return prs;
    }

    public void setPrs(ProjectService prs) {
        this.prs = prs;
    }

    public void project(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String flag = request.getParameter("flag");
        JSONObject json = new JSONObject();
        System.out.println(flag+"***");
        if("getAll".equals(flag)){
            String Teacher_id = ((TeacherBean)request.getSession().getAttribute("教师")).getTeacher_id();
            List<ProjectBean> projectList = prs.getAll(Teacher_id);
            json.put("getAll",projectList);
            System.out.println(json.toJSONString());
        }
        if("studentGetAll".equals(flag)){
            List<ProjectBean> projectList = prs.getAll("");
            json.put("studentGetAll",projectList);
            System.out.println(json.toJSONString());
        }
        if("add".equals(flag)){
            ProjectBean pb =new ProjectBean();
            pb.setProject_name(request.getParameter("name"));
            pb.setProject_introduction(request.getParameter("introduction"));
            pb.setTeacher_id(request.getParameter("teacherID"));
            pb.setProject_isSet("否");
            pb.setStudent_id(((StudentBean)request.getSession().getAttribute("学生")).getStudent_id());
            json.put("msg",prs.add(pb));
        }
        if("delete".equals(flag)){

        }
        if("change".equals(flag)){

        }
        if("set".equals(flag)){
            String projectID = request.getParameter("projectID");
            ProjectBean pb = prs.getById(projectID);
            json.put("project",pb);
            System.out.println(json.toJSONString());
        }
        if("isSet".equals(flag)){
            boolean setTeam = false;
            String id = request.getParameter("id");
            ProjectBean pb = prs.getById(id);
            String isSet=request.getParameter("isSet");
            if("是".equals(isSet)&&"否".equals(pb.getProject_isSet())){
                setTeam=true;
            }
            String classify=request.getParameter("classify");
            pb.setProject_isSet(isSet);
            pb.setProjectClassify_id(classify);
            json.put("msg",prs.change(pb,setTeam));
        }
        if("see".equals(flag)){
            String id = request.getParameter("projectID");
            json.put("project",prs.getById(id));
            System.out.println(json.toJSONString());
        }
        response.getWriter().print(json.toJSONString());
    }
}
